/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.cloudfarming.client.filepicker;

import java.awt.Cursor;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.ServiceLoader;
import javax.swing.JFrame;
import javax.swing.RepaintManager;
import nl.cloudfarming.sensor.AlreadyParsedException;
import nl.cloudfarming.sensor.FileReadException;
import nl.cloudfarming.sensor.Receiver;
import nl.cloudfarming.util.filecopier.Exception.DirectoryCreationException;
import nl.cloudfarming.util.filecopier.Exception.FileMoveException;
import nl.cloudfarming.util.filecopier.Exception.NotADirectoryException;
import nl.cloudfarming.util.filecopier.Exception.NotAFileException;
import nl.cloudfarming.util.filecopier.ParserFileCopier;
import org.openide.awt.StatusDisplayer;
import org.openide.windows.WindowManager;

/**
 *
 * @author Merijn Zengers, Gerben Feenstra
 */
public class GenericFilePicker {

    private ParserFileCopier copier = new ParserFileCopier();
    private final JFrame mainWin = (JFrame) WindowManager.getDefault().getMainWindow();

    protected void sendToReceivers(List <File> sensorFiles) throws NoSuchAlgorithmException, IOException, FileReadException, AlreadyParsedException, NotADirectoryException, NotAFileException, DirectoryCreationException, FileNotFoundException, FileMoveException{
        ServiceLoader<Receiver> receiverLoader = ServiceLoader.load(Receiver.class);
        for (Receiver receiver : receiverLoader) {
            for(File sensorFile : sensorFiles){
                if(receiver.canReceive(sensorFile)){
                    receiver.receive(sensorFile);
                }
            }
            if(receiver.isReceiving()){
                receiver.start();
            }
        }
    }

    protected void sendToReceivers(File sensorFile) throws NoSuchAlgorithmException, IOException, FileReadException, AlreadyParsedException, NotADirectoryException, NotAFileException, DirectoryCreationException, FileNotFoundException, FileMoveException{
        ServiceLoader<Receiver> receiverLoader = ServiceLoader.load(Receiver.class);
        for (Receiver receiver : receiverLoader) {
            if(receiver.canReceive(sensorFile)){
                receiver.receive(sensorFile);
                receiver.start();
            }
        }
    }

    protected void lockGui(){
        // set status text
        StatusDisplayer.getDefault().setStatusText("");
        RepaintManager.currentManager(mainWin).paintDirtyRegions();

        // set wait cursor to glasspane after making it visible
        mainWin.getGlassPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        mainWin.getGlassPane().setVisible(true);
    }

    protected void freeGui(){
             // clear status text
        StatusDisplayer.getDefault().setStatusText(""); // NOI18N
        // clear wait cursor
        mainWin.getGlassPane().setCursor(Cursor.getDefaultCursor());
        mainWin.getGlassPane().setVisible(false);
    }



}
